package it.unibo.convoycruisecontrol.chiefboatsubsystem;

import it.unibo.location.ILocation;
import it.unibo.location.LocationDevice;
import it.unibo.util.information.Information;
import it.unibo.util.information.InformationConverter;

/**
 * @author ale
 * @version 1.0
 * @created 24-ott-2012 00:47:07
 */
public abstract class NavigationInfoReceiver extends it.unibo.contact.chiefBoatSubSystem.NavigationInfoReceiverSupport {

	protected int boatsNumber;
	protected ILocation[] locations = null;
	protected float[] speeds = null;
	protected int lastInfoReceived;
	protected LocationDevice locationDevice;		
	protected InformationConverter informationConverter;

	public NavigationInfoReceiver() throws Exception
	{
		super();
	}

	public NavigationInfoReceiver(int boatsNumber, LocationDevice locationDevice, InformationConverter informationConverter) throws Exception
	{
		super();
		if(boatsNumber < 0)
			throw new IllegalArgumentException("The number of the boats cannot be negative");
		if(locationDevice == null || informationConverter == null)
			throw new IllegalArgumentException("The parameters cannot be null");

		this.boatsNumber = boatsNumber;
		this.locationDevice = locationDevice;
		this.informationConverter = informationConverter;
		locations = new ILocation[boatsNumber];
		speeds = new float[boatsNumber];
	}

	protected void init()
	{
		locations = new ILocation[boatsNumber];
		speeds = new float[boatsNumber];
	}

	@Override
	protected void extractInfo(String message) throws Exception 
	{
		if(locations == null)
			init();

		Information boatInfo = informationConverter.toInformation(message);

		int seqNumber = Integer.parseInt(boatInfo.getAttributes().get("sequenceNumber"));		
		float speed = Float.parseFloat(boatInfo.getAttributes().get("speed"));
		double latitude = Double.parseDouble(boatInfo.getAttributes().get("latitude"));
		double longitude = Double.parseDouble(boatInfo.getAttributes().get("longitude"));

		if(seqNumber < locations.length)
		{
			locations[seqNumber] = locationDevice.createLocation(latitude, longitude, 0);
			speeds[seqNumber] = speed;
			lastInfoReceived = seqNumber;
		}
	}

	protected abstract void newInformations();

}